<template>
  <div class="login-container">
    <a-row class="login-row">
      <a-col :span="12" class="login-right">
        <div class="login-logo enter-left">
          <img src="@/assets/login-box-bg.svg" />
          <h1 style="font-weight:300">后台管理系统</h1>
        </div>
      </a-col>
      <a-col :span="12">
        <div class="login-box enter-right" @keyup.enter="onSubmit">
          <div style="text-align: center">
            <h3 class="login-title">登录</h3>
          </div>
          <a-form :model="form" layout="vertical" :rules="rules" ref="formRef" size="large">
            <a-form-item name="userName">
              <a-input v-model:value="form.userName" size="large">
                <template #prefix>
                  <user-outlined type="user" />
                </template>
              </a-input>
            </a-form-item>

            <a-form-item name="userPassword">
              <a-input-password v-model:value="form.userPassword"  size="large">
                <template #prefix>
                  <LockOutlined />
                </template>
              </a-input-password>
            </a-form-item>
          </a-form>

          <div>
            <a-button
              size="large"
              type="primary"
              @click="onSubmit"
              style="width: 100%;background:#2645b1;border:1px solid #2645b1"
              :loading="loading"
              >登录</a-button
            >
          </div>

          <!-- <div class="test-demo">
            <div class="light" @click="onSubmit">
              <div></div>
              <div></div>
              <div></div>
              <div></div>
              登录
            </div>
          </div> -->
        </div>
      </a-col>
    </a-row>
  </div>
</template>

<script lang="ts">
import { ref, reactive, defineComponent, toRefs, UnwrapRef, toRaw } from 'vue';
import { UserOutlined, EyeOutlined ,LockOutlined} from '@ant-design/icons-vue';
import { ValidateErrorEntity } from 'ant-design-vue/es/form/interface';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
import { message ,notification} from 'ant-design-vue';
interface userState {
  userName: String;
  userPassword: String;
}

export default defineComponent({
  components: { UserOutlined, EyeOutlined,LockOutlined },
  setup(props, context) {
    const store = useStore();
    const router = useRouter();
    const loading = ref<boolean>(false);
    const form: UnwrapRef<userState> = reactive({
      userName: 'admin',
      userPassword: 'qwedsa123',
    });
    const rules = {
      userName: [
        {
          required: true,
          message: '请填写用户名',
          trigger: 'blur',
        },
        // { min: 3, max: 5, message: "Length should be 3 to 5", trigger: "blur" },
      ],
      userPassword: [
        {
          required: true,
          message: '请填写密码',
          trigger: 'blur',
        },
      ],
    };
    const formRef = ref();
    const onSubmit = () => {
      formRef.value
        .validate()
        .then(() => {
          loading.value = true;

          store
            .dispatch('USER_LOGIN', toRaw(form))
            .then(res => {
              setTimeout(() => {
                loading.value = false;
                router.replace({ name: 'admin' });
                notification.success({
                  message:'登录成功'
                })
              }, 1000);
            })
            .catch(() => {
              loading.value = false;
            });
        })
        .catch((error: ValidateErrorEntity<userState>) => {
          console.log('error', error);
        });
    };
    return {
      form,
      rules,
      onSubmit,
      formRef,
      loading,
    };
  },
});
</script>

<style lang="less" scoped>
.test-demo {
  // width: 100%;
  // height: 200px;
  // display: flex;
  // justify-content: center;
  // align-items: center;
  // background: #050901;
  .light {
    position: relative;
    padding: 10px 30px;
    text-align: center;
    color: #03e9f4;
    font-size: 18px;
    text-transform: uppercase;
    transition: 0.5s;
    letter-spacing: 30px;
    cursor: pointer;
    overflow: hidden;
    filter: hue-rotate(45deg);
    &:hover {
      background-color: #03e9f4;
      color: #fff;
      // box-shadow: 0 0 5px #03e9f4, 0 0 25px #03e9f4, 0 0 50px #03e9f4, 0 0 200px #03e9f4;
      box-shadow: 0 0 5px #03e9f4, 0 0 5px #03e9f4, 0 0 5px #03e9f4, 0 0 200px #03e9f4;
    }
    > div {
      position: absolute;
    }
    > div:nth-child(1) {
      width: 100%;
      height: 2px;
      top: 0;
      left: -100%;
      background: linear-gradient(to right, transparent, #03e9f4);
      animation: animate1 2s linear infinite;
    }
    > div:nth-child(2) {
      width: 2px;
      height: 100%;
      top: -100%;
      right: 0;
      background: linear-gradient(to bottom, transparent, #03e9f4);
      animation: animate2 2s linear infinite;
      animation-delay: 0.25s;
    }
    > div:nth-child(3) {
      width: 100%;
      height: 2px;
      bottom: 0;
      right: -100%;
      background: linear-gradient(to left, transparent, #03e9f4);
      animation: animate3 2s linear infinite;
      animation-delay: 0.5s;
    }
    > div:nth-child(4) {
      width: 2px;
      height: 100%;
      bottom: -100%;
      left: 0;
      background: linear-gradient(to top, transparent, #03e9f4);
      animation: animate4 2s linear infinite;
      animation-delay: 0.75s;
    }
  }
}
@keyframes animate1 {
  0% {
    left: -100%;
  }
  50%,
  100% {
    left: 100%;
  }
}
@keyframes animate2 {
  0% {
    top: -100%;
  }
  50%,
  100% {
    top: 100%;
  }
}
@keyframes animate3 {
  0% {
    right: -100%;
  }
  50%,
  100% {
    right: 100%;
  }
}
@keyframes animate4 {
  0% {
    bottom: -100%;
  }
  50%,
  100% {
    bottom: 100%;
  }
}
.enter-right{
  >div:nth-child(1){
    animation-fill-mode: forwards;
    animation: enter-right .5s linear;
  }
  >form{
    animation-fill-mode: forwards;
    animation: enter-right .6s linear;
  }
  button{
    animation-fill-mode: forwards;
    animation: enter-right .7s linear;
  }
}

@keyframes enter-right{
  0%{
    transform: translateX(100%);
    opacity: 0;
  }
  50%{
    transform: translateX(50%);
    opacity: 0.5;
  }
  100%{
    transform: translateX(0);
    opacity:1;
  }
}
.enter-left{
  
  >img{
    animation-fill-mode: forwards;
    animation: enter-left .5s linear;
    // animation-delay: 1s;
  }
  >h1{
    animation-fill-mode: forwards;
    animation: enter-left .6s linear;
    // animation-delay: .1s;
  }
}

@keyframes enter-left{
  0%{
    transform: translateX(-110%);
    opacity: 0;
  }
  50%{
    transform: translateX(-50%);
    opacity: 0.5;
  }
  100%{
    transform: translateX(0px);
    opacity:1;
  }
}
.login-row {
  height: 100%;
  .login-logo {
     z-index:10;
     width:45%;
    h1 {
      color: #fff;
      margin-top: 50px;
    }
    img {
      width: 70%;
    }
  }
  .login-right{
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .login-right::before {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url('../assets/login-bg.svg');
    background-position: 100%;
    background-repeat: no-repeat;
    background-size: auto 100%;
    content: '';
  }
}

@keyframes loginCss {
  from {
    top: 0%;
  }

  to {
    top: 50%;
  }
}
.login-container {
  width: 100%;
  height: 100vh;
  .login-box {
    // animation: loginCss 1s infinite ease;
    // animation-iteration-count: 1;
    width: 60%;
    color: #fff;
    position: absolute;
    transform: translate(-50%, -50%);
    // border: 1px solid #999;
    padding: 30px;
    border-radius: 10px;
    left: 50%;
    top: 50%;
    // background: #fafafa;
    .login-title {
      transition: all 0.3s linear;
      letter-spacing: 10px;
      font-size:30px;
      // font-weight: 400;
      text-align: left;
      margin-bottom: 20px;
      // &:hover {
      //   transform: scale(1.3);
      //   font-weight: 600;
      // }
    }
    > div {
      margin-top: 20px;
    }
    .form-label {
      transition: all 0.2s linear;
      &:hover {
        transform: scale(1.1, 1.1);
        filter: contrast(130%);
      }
      //   font-size:12px
    }
    .ipt {
      outline: none;
      background-color: transparent;
      border-style: none;
      color: rgba(0, 0, 0, 0.87);
      max-height: 32px;
      line-height: 20px;
      padding: 8px 0;
    }
  }
}
</style>